package com.mayikt.servlet;

import com.mayikt.entity.FlightEntity;
import com.mayikt.service.FlightService;
import com.mayikt.utils.DateUtils;
import com.mayikt.utils.MayiktJdbcUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;


@WebServlet("/insertFlight")
public class InsertFlightServlet extends HttpServlet {
    private FlightService flightService = new FlightService();
    
    /**
    * 跳转到 添加 航班数据页面
    *
    * @param req
    * @param resp
    * @throws ServletException
    * @throws IOException
    */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("InsertFlight.jsp").forward(req, resp);
    }
    
    /**
    * 接受 插入航班数据请求
    *
    * @param req
    * @param resp
    * @throws ServletException
    * @throws IOException
    */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            String flightId = req.getParameter("flightId");
            if (StringUtils.isEmpty(flightId)) {
                req.setAttribute("errorMsg", "flightId的值不能够为空!");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }
            String company = req.getParameter("company");
            if (StringUtils.isEmpty(company)) {
                req.setAttribute("errorMsg", "company的值不能够为空!");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }
            String departureAirport = req.getParameter("departureAirport");
            if (StringUtils.isEmpty(departureAirport)) {
                req.setAttribute("errorMsg", "departureAirport的值不能够为空!");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }
            String arriveAirport = req.getParameter("arriveAirport");
            if (StringUtils.isEmpty(departureAirport)) {
                req.setAttribute("errorMsg", "arriveAirport的值不能够为空!");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }
            // 将日期string类型转化成date
            String departureTimeStr = req.getParameter("departureTime");
            if (StringUtils.isEmpty(departureTimeStr)) {
                req.setAttribute("errorMsg", "departureTime的值不能够为空!");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }
            Date departureTime = DateUtils.stringToDate(departureTimeStr);
            String arriveTimeStr = req.getParameter("arriveTime");
            if (StringUtils.isEmpty(departureTimeStr)) {
                req.setAttribute("errorMsg", "arriveTime的值不能够为空!");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }
            Date arriveTime = DateUtils.stringToDate(arriveTimeStr);
            String model = req.getParameter("model");
            FlightEntity flightEntity = new FlightEntity();
            flightEntity.setFlightId(flightId);
            flightEntity.setDepartureAirport(departureAirport);
            flightEntity.setArriveAirport(arriveAirport);
            flightEntity.setDepartureTime(departureTime);
            flightEntity.setArriveTime(arriveTime);
            flightEntity.setModel(model);
            flightEntity.setCompany(company);
            int result = flightService.insertFlight(flightEntity);
//            if (result <= 0) {
//                // 修改失败的情况下
//                req.setAttribute("errorMsg", "插入失败!");
//                req.getRequestDispatcher("error.jsp").forward(req, resp);
//                return;
//            }
            if(result > 0){
                // 如果插入成功的情况下  则重定向到showFlight 查询航班信息
                req.getRequestDispatcher("showFlight").forward(req,resp);
                //resp.sendRedirect("show.jsp");
            }else {
                // 修改失败的情况下
                req.setAttribute("errorMsg", "插入失败!");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }

        } catch (Exception e) {
            e.printStackTrace();
            // 修改失败的情况下
            req.setAttribute("errorMsg", "系统错误!");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
        }


    }



}

